﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

using Microsoft.Xna.Framework.Media;

namespace Objects
{
    public class Player : BaseObject
    {
        SpriteBatch spriteBatch;
        float FireTimer = 1;
        float FireCounter = 0;
        bool CanFire = false;
        public int PlayerScore = 0;
        public Player()
        {
            this.TheObjectType = ObjectType.Player;
            Image = States.Gameplay.Instance.theTextureManager.GetTexture("TANK");
            Position = new Vector2(300, 300);
            spriteBatch = TopDownShooter.Game1.spriteBatch;
            Velocity = new Vector2(100, 100);
        }

        public void GetInput(float ElapsedTime)
        {
            if (Keyboard.GetState().IsKeyDown(Keys.W))
            {
                Position.Y -= Velocity.Y * ElapsedTime;
                Orientation = 0;
            }
            else if (Keyboard.GetState().IsKeyDown(Keys.S))
            {
                Position.Y += Velocity.Y * ElapsedTime;
                Orientation = 180;
            }
            else if (Keyboard.GetState().IsKeyDown(Keys.A))
            {
                Position.X -= Velocity.X * ElapsedTime;
                Orientation = 270;
            }
            else if (Keyboard.GetState().IsKeyDown(Keys.D))
            {
                Position.X += Velocity.Y * ElapsedTime;
                Orientation = 90;
            }

            if (Keyboard.GetState().IsKeyDown(Keys.Space))
            {
                if (CanFire)
                {
                    Objects.Bullet newBullet = new Bullet(this);
                    States.Gameplay.Instance.theObjectManager.AddObject(newBullet);

                    CanFire = false;
                    FireCounter = 0;
                   
                }

            }
        }
        public override void Update(float ElapsedTime)
        {
            FireCounter += ElapsedTime;
            if (FireCounter >= 1)
            {
                CanFire = true;
            }
            GetInput(ElapsedTime);
        }

        public override void Render(float ElapsedTime)
        {
            spriteBatch.Draw(Image, Position, null, Color.White, MathHelper.ToRadians(Orientation), new Vector2(Image.Width * 0.5f, Image.Height *0.5f), 1.0f, SpriteEffects.None, 0);
        }
        public override void CheckCollision(BaseObject theObject)
        {

        }
    }
}